﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Modules;
using Newtonsoft.Json;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Logging.Repositories;
using PmSoft.Repositories;

namespace PmSoft.Logging;

/// <summary>
/// 操作日志业务逻辑类
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class SysTableLogService
{
    private readonly IApplicationContext applicationContext;
    private readonly SysTableLogRepository repository;
    private readonly static JsonSerializerSettings serializerSettings = new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore };
    /// <summary>
    /// 构造函数 
    /// </summary>
    /// <param name="repository"></param>
    public SysTableLogService(IApplicationContext applicationContext, SysTableLogRepository repository)
    {
        this.applicationContext = applicationContext;
        this.repository = repository;
    }

    /// <summary>
    /// 删除指定时间段内的日志列表 
    /// </summary>
    /// <param name="startDate"></param>
    /// <param name="endDate"></param>
    /// <returns></returns>
    public int Clean(DateTime? startDate, DateTime? endDate)
    {
        return repository.Clean(startDate, endDate);
    }

    /// <summary>
    /// 创建操作日志
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public long Create(EntityEventArgs eventArgs)
    {
        if (eventArgs.User == null)
            return 0;
        var entry = new SysTableLog(eventArgs.User);
        var descriptionAttribute = eventArgs.Entity.GetType().GetCustomAttribute<DescriptionAttribute>();
        if (descriptionAttribute != null)
            entry.TableName = descriptionAttribute.Description;
        else
            entry.TableName = eventArgs.Entity.GetType().Name;

        entry.ObjectId = eventArgs.Entity.EntityId.ToString() ?? string.Empty;
        entry.Data = JsonConvert.SerializeObject(eventArgs.Entity, serializerSettings);
        entry.OperationType = eventArgs.OperationType;
        entry.Ip = eventArgs.ClientIp;
        entry.Url = eventArgs.Url;
        entry.DatabaseName = eventArgs.DatabaseName;
        repository.Insert(entry);
        return entry.Id;
    }


    /// <summary>
    /// 删除entryId相应的操作日志 
    /// </summary>
    /// <param name="entryId"></param>
    public void Delete(int entryId)
    {
        repository.DeleteByEntityId(entryId);
    }

    /// <summary>
    /// 根据DiscussQuestionQuery查询获取可分页的数据集合 
    /// </summary>
    /// <param name="query"></param>
    /// <param name="pageSize"></param>
    /// <param name="pageIndex"></param>
    /// <returns></returns>
    public IPagedList<SysTableLog> GetLogs(SysTableLogQuery query, int pageIndex, int pageSize)
    {
        return repository.GetLogs(query, pageIndex, pageSize);
    }

    /// <summary>
    /// 根据id获取操作日志
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public SysTableLog? GetLog(int id)
    {
        return repository.Get(id);
    }

}

